/*
 * File:   UserDelay.c
 * Author: Honrun
 */
#include "stdint.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"

#include "DevicesTime.h"
#include "DevicesDelay.h"

/*
 * Return:      void
 * Parameters:  void
 * Description: DWT初始化
 */
void vTimerDelayInit(void)
{
}

/*
 * Return:      void
 * Parameters:  Time: 延时时间
 * Description: 秒延时
 */
void vTimerDelayS(float fTime)
{
    vTimerDelayUs(fTime * 1000000.0f);
}

/*
 * Return:      void
 * Parameters:  Time: 延时时间
 * Description: 毫秒延时
 */
void vTimerDelayMs(float fTime)
{
    vTimerDelayUs(fTime * 1000.0f);
}

/*
 * Return:      void
 * Parameters:  Time: 延时时间
 * Description: 微秒延时
 */
void vTimerDelayUs(float fTime)
{
    int64_t lTimeStop = lTimebaseGet() + (int64_t)fTime;

    while(lTimebaseGet() < lTimeStop);
}

/*
 * Return:      void
 * Parameters:  Time: 延时时间
 * Description: 秒延时
 */
void vRtosDelayS(float fTime)
{
    vRtosDelayMs(fTime * 1000.0f);
}

/*
 * Return:      void
 * Parameters:  Time: 延时时间
 * Description: 毫秒延时
 */
void vRtosDelayMs(float fTime)
{
    if(fTime < 1.0f)
        return;

    if(xTaskGetSchedulerState() == taskSCHEDULER_RUNNING)
        vTaskDelay(fTime);
    else
        vDelayMs(fTime);
}
